package org.easy.tool.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.easy.tool.common.RandomUtil;
import org.easy.tool.common.TextUtil;
import org.easy.tool.common.TimeUtil;
import org.easy.tool.entity.Problem;
import org.easy.tool.entity.User;
import org.easy.tool.mapper.ProblemMapper;
import org.easy.tool.service.IProblem;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Valley
 * @since 2023-08-25 14:20:43
 */
@Service
public class ProblemService extends ServiceImpl<ProblemMapper, Problem> implements IProblem {

    @Autowired
    ProblemMapper problemMapper;

    @Override
    public Problem getProblemById(String problemId) {
        LambdaQueryWrapper<Problem> lambda = new LambdaQueryWrapper<>();
        lambda.eq(Problem::getId, problemId);
        return getOne(lambda);
    }

    @Override
    public List<Problem> getProblemListByTitle(String title) {
        LambdaQueryWrapper<Problem> lambda = new LambdaQueryWrapper<>();
        lambda.like(Problem::getTitle, title);
        return problemMapper.selectList(lambda);
    }

    @Override
    public String addProblem(Problem problem) {
        problem.setCreateTime(TimeUtil.now());
        problem.setUpdateTime(TimeUtil.now());
        save(problem);
        return problem.getId();
    }

    @Override
    public Problem addGapProblem(String knowledge) {
        List<String> list = TextUtil.analysisFilter(knowledge);
        List<Integer> numList = RandomUtil.randomIntList(4, list.size());
        List<String> strList = numList.stream().map(list::get).collect(Collectors.toList());
        for (String s : strList) {
            knowledge = knowledge.replace(s, "____");
        }
        Problem problem = new Problem();
        problem.setTitle(knowledge);
        problem.setContent(String.join(";", strList));
        return problem;
    }
}
